package qlsl.androiddesign.view.subview.activityview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import qlsl.androiddesign.activity.subactivity.RecruitDetailActivity;
import qlsl.androiddesign.adapter.subadapter.CarDetailAdapter;
import qlsl.androiddesign.adapter.subadapter.RecruitDetailAdapter;
import qlsl.androiddesign.entity.otherentity.RecruitJob;
import qlsl.androiddesign.http.service.commonservice.CommentService;
import qlsl.androiddesign.http.service.subservice.ForumService;
import qlsl.androiddesign.http.service.subservice.RecruitDetailService;
import qlsl.androiddesign.library.pulltorefresh.PullToRefreshScrollView;
import qlsl.androiddesign.listener.OnDefaultRefreshListener;
import qlsl.androiddesign.util.commonutil.ImageUtils;
import qlsl.androiddesign.util.commonutil.TimeFormatUtils;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 招聘详情页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class RecruitDetailView extends FunctionView<RecruitDetailActivity> {

	private PullToRefreshScrollView refreshView;

	private ListView listView_first, listView_second;

	private RecruitJob item;

	public RecruitDetailView(RecruitDetailActivity activity) {
		super(activity);
		setContentView(R.layout.activity_recruit_detail);
	}

	protected void initView(View view) {
		setTitle("详情");
		item = (RecruitJob) activity.getIntent().getSerializableExtra("data");
		refreshView = findViewById(R.id.refreshScrollView);
		listView_first = findViewById(R.id.listView);
		listView_second = (ListView) findViewById(R.id.listViewSecondParent).findViewById(R.id.listView);
	}

	protected void initData() {
		setDetailData();
		RecruitDetailService.queryRecruitDetailList(item, this, activity);
		// 设置浏览量
		

	}

	protected void setDetailData() {
		ImageView iv_icon = findViewById(R.id.iv_icon);
		TextView tv_name = findViewById(R.id.tv_name);
		TextView tv_time = findViewById(R.id.tv_time);
		TextView tv_section = findViewById(R.id.tv_section);
		TextView tv_sec_title = findViewById(R.id.tv_sec_title);
		TextView tv_num_view = findViewById(R.id.tv_num_view);
		TextView tv_num_comment = findViewById(R.id.tv_num_comment);

		if (item != null) {
			ImageUtils.circle(activity, item.getHeadphoto(), iv_icon);
			tv_name.setText(item.getUsername());
			tv_time.setText(TimeFormatUtils.format(item.getTime()));
			tv_section.setText("[" + item.getProvince() + "" + item.getCity() + "]");
			tv_sec_title.setText(item.getTitle());
			tv_num_view.setText("浏览" + item.getViews());
			tv_num_comment.setText("评论" + (item.getRows() == null ? 0 : item.getRows().size()));
		}

		// //////////////////////////////第一个listview/////////////////////////////////////////////////
		// listView_first
		String[] names = new String[] { "招聘地区", "驾驶时长", "标准薪资", "工作经验", "操作方向", "福利要求", "联系方式" };
		String[] contents = new String[] { item.getProvince() + item.getCity(), item.getDrivertime(), item.getPay(),
				item.getWorkhistory(), item.getPlaydirection(), item.getWelfare(), item.getMobile() };

		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

		for (int index = 0; index < names.length; index++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("name", names[index]);
			map.put("content", contents[index]);
			list.add(map);
		}
		BaseAdapter adapter = new CarDetailAdapter(activity, list);
		listView_first.setAdapter(adapter);

	}

	protected void initListener() {
		refreshView.setOnRefreshListener(new OnDefaultRefreshListener<ScrollView>());
	}

	public <T> void showData(T... t) {
		if (t[0] instanceof RecruitJob) {
			item = (RecruitJob) t[0];
			notifyDataSetChanged();
			ForumService.read(item.getAdvertisesid() + "", 2, this, activity);
		} else if (t[0] instanceof String) {
			item.setViews(item.getViews() + 1);
			setDetailData();
		}
	}

	private void notifyDataSetChanged() {
		RecruitDetailAdapter adapter = new RecruitDetailAdapter(activity, item.getRows());
		listView_second.setAdapter(adapter);
		refreshView.onRefreshComplete();
	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.reply_send:
			doClickReplySendView(view);
			break;
		}
	}

	private void doClickReplySendView(View view) {
		Integer id = Integer.parseInt(item.getAdvertisesid());
		EditText reply_content = (EditText) ((ViewGroup) view.getParent()).findViewById(R.id.reply_content);
		String content = reply_content.getText().toString();
		reply_content.setText(null);
		RecruitJob item_old = (RecruitJob) activity.getIntent().getSerializableExtra("data");
		CommentService.createComment(item_old, id + "", content, this, activity);
	}

}
